package com.ciaojian.bytedance.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ciaojian.bytedance.service.ByteDanceGoodsService;
import com.ciaojian.bytedance.vo.bytedance.*;
import com.ciaojian.core.mapper.DyOrderMapper;
import com.ciaojian.core.model.Order;
import com.ciaojian.core.model.dy.DyOrder;
import com.ciaojian.core.service.UserMemberService;
import com.ciaojian.core.util.Result;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@ApiModel("抖音团购商品接口")
@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private ByteDanceGoodsService byteDanceGoodsService;

    @Resource
    private DyOrderMapper dyOrderMapper;

    @ApiOperation("应用添加/修改商品")
    @PostMapping("/cs2")
    public JSONObject saveGoodsInfo(@RequestBody ReceiveOrderQuery receiveOrderQuery) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();

        Integer a= 456;
        if ("456".equals(a)){
            System.out.println(1);
        }

        JsonNode msgNode = objectMapper.readTree(receiveOrderQuery.getMsg());


        DyOrder dyOrder = dyOrderMapper.selectById(778902);

        JsonNode coupon_code_id = msgNode.get("cp_extra");
        JsonNode jsonNode = objectMapper.readTree(coupon_code_id.toString());
        int memberId = jsonNode.has("member_id") ? jsonNode.get("member_id").asInt() : 0;
        dyOrder.setMemberId(memberId);

        return null;
    }

    @ApiOperation("应用添加/修改商品")
    @PostMapping("/save")
    public JSONObject saveGoodsInfo(@RequestBody ProductSavaOrUpdateVo productSavaOrUpdateVo) throws Exception {
        return byteDanceGoodsService.saveGoodsInfo(productSavaOrUpdateVo);
    }

    @ApiOperation("潮剪后台修改商品")
    @PostMapping("/update")
    public JSONObject updateGoodsInfo(@RequestBody ProductUpdateVo productUpdateVo) throws Exception {
        return byteDanceGoodsService.updateGoodsInfo(productUpdateVo);
    }

    /**
     * 免审修改商品
     */
    @ApiOperation("免审核修改商品")
    @GetMapping("/savefreeaudit")
    public JSONObject savefreeaudit(@ApiParam("抖音商品号")@RequestParam String productId ,@ApiParam("销售结束时间")@RequestParam String soldEndtime,@ApiParam("数量") @RequestParam String stockQty) throws Exception {
        return byteDanceGoodsService.savefreeaudit(productId,soldEndtime,stockQty);

    }


    /**
     * categoryId : 丽人17000000 美发17001000 17001001
     * 商品类型 1 : 团购套餐 3 : 预售券 4 : 日历房 5 : 门票 7 : 旅行跟拍 8 : 一日游 11 : 代金券
     */
    @ApiModelProperty("获取抖音商品模板")
    @GetMapping("/getGoodsTemplate")
    public JSONObject getgoodstemplate(@ApiParam("行业分类id") @RequestParam String categoryId,@ApiParam("商品类型id")@RequestParam String productType) throws Exception {
        return byteDanceGoodsService.getgoodstemplate(categoryId,productType);
    }

    @ApiModelProperty("查询商品线上数据列表")
    @GetMapping("/product/list/")
    public Result productList(@ApiParam("第一页不传，之后用前一次返回的next_cursor传入进行翻页") @RequestParam(required = false) String cursor,
                              @ApiParam("分页数量，不传默认为5") @RequestParam(required = false,defaultValue = "10") int count,
                              @ApiParam("过滤在线状态 1-在线 2-下线 3-封禁") @RequestParam(required = false,defaultValue = "1") int status) throws Exception {
        return byteDanceGoodsService.productList(cursor,count,status);
    }

    /**
     * 抖音格式
     * @param productIds
     * @param outIds
     * @return
     * @throws Exception
     */
    @ApiModelProperty("查询商品线上数据(抖音原数据")
    @GetMapping("/productOne/get/")
    public JSONObject productOneGet(@ApiParam("抖音商品id") @RequestParam(required = false) String productIds,
                                    @ApiParam("内部商品id 同上一样") @RequestParam(required = false) String outIds) throws Exception {
        return byteDanceGoodsService.productOneGet(productIds,outIds);
    }

    @ApiModelProperty("查询商品线上数据")
    @GetMapping("/product/get/")
    public Result productGet(@ApiParam("抖音商品id") @RequestParam(required = false) String productIds,
                             @ApiParam("内部商品id 同上一样") @RequestParam(required = false) String outIds) throws Exception {
        return byteDanceGoodsService.productGet(productIds,outIds);
    }


    @ApiModelProperty("查询商品草稿数据列表")
        @GetMapping("/product/draft/")
    public JSONObject productDraft() throws Exception {
        return byteDanceGoodsService.productDraft();
    }

    @ApiModelProperty("商品sku同步")
    @PostMapping("/sync")
    public JSONObject syncStore(@RequestBody ByteDanceSyncGoodsSkuVo byteDanceSyncGoodsSkuVo) {

        return byteDanceGoodsService.syncStore(byteDanceSyncGoodsSkuVo);


    }


    @ApiModelProperty("查询草稿商品线上数据")
    @GetMapping("/draft/get/")
    public Result draftList(@ApiParam("抖音商品id") String productIds) throws Exception {
        return byteDanceGoodsService.draftList(productIds);

    }


    @ApiModelProperty("抖音商品上下架")
    @GetMapping("/operate")
    public JSONObject operate(@ApiParam("抖音商品id") @RequestParam(required = false) String productId,int type) throws Exception {
        return byteDanceGoodsService.operate(productId,type);

    }



}
